﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CuitWinTable.cs" company="">
//   
// </copyright>
// <summary>
//   The cuit win table.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.CUIT.CuitControls.WinControls
{
    using System;
    using System.Linq;

    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UITesting.WinControls;

    /// <summary>
    ///     The cuit win table.
    /// </summary>
    public class CuitWinTable : CuitWinControl<WinTable>
    {
        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="CuitWinTable" /> class.
        /// </summary>
        public CuitWinTable()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinTable"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        public CuitWinTable(UITestControl testControl)
            : base(testControl)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinTable"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitWinTable(UITestControl testControl, string searchProperties)
            : base(testControl, searchProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinTable"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        public CuitWinTable(UITestControl testControl, string searchProperties, string filterProperties)
            : base(testControl, searchProperties, filterProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinTable"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitWinTable(ICuitControlBase wrappedControl, string searchProperties)
            : base(wrappedControl, searchProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinTable"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        public CuitWinTable(ICuitControlBase wrappedControl, string searchProperties, string filterProperties)
            : base(wrappedControl, searchProperties, filterProperties)
        {
        }

        #endregion

        #region Enums

        /// <summary>
        ///     The cuit win table search options.
        /// </summary>
        public enum CuitWinTableSearchOptions
        {
            /// <summary>
            ///     The normal.
            /// </summary>
            Normal, 

            /// <summary>
            ///     The normal tight.
            /// </summary>
            NormalTight, 

            /// <summary>
            ///     The greedy.
            /// </summary>
            Greedy, 

            /// <summary>
            ///     The starts with.
            /// </summary>
            StartsWith, 

            /// <summary>
            ///     The ends with.
            /// </summary>
            EndsWith
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets the cells.
        /// </summary>
        public UITestControlCollection Cells
        {
            get
            {
                return this._control.Cells;
            }
        }

        /// <summary>
        ///     Gets the column count.
        /// </summary>
        public int ColumnCount
        {
            get
            {
                this._control.WaitForControlReady();
                return this._control.ColumnHeaders.Count;
            }
        }

        /// <summary>
        ///     Gets the row count.
        /// </summary>
        public int RowCount
        {
            get
            {
                this._control.WaitForControlReady();
                return this._control.Rows.Count;
            }
        }

        /// <summary>
        ///     Gets the rows.
        /// </summary>
        public UITestControlCollection Rows
        {
            get
            {
                return this._control.Rows;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The click on cell.
        /// </summary>
        /// <param name="rowIndex">
        /// The row index.
        /// </param>
        /// <param name="columnIndex">
        /// The column index.
        /// </param>
        public void ClickOnCell(int rowIndex, int columnIndex)
        {
            this._control.WaitForControlReady();
            var cell = new CuitWinCell(this._control, rowIndex, columnIndex);
            cell.Click();
        }

        /// <summary>
        /// The double click on cell.
        /// </summary>
        /// <param name="rowIndex">
        /// The row index.
        /// </param>
        /// <param name="columnIndex">
        /// The column index.
        /// </param>
        public void DoubleClickOnCell(int rowIndex, int columnIndex)
        {
            this._control.WaitForControlReady();
            var cell = new CuitWinCell(this._control, rowIndex, columnIndex);
            cell.DoubleClick();
        }

        /// <summary>
        /// The get cell child.
        /// </summary>
        /// <param name="rowIndex">
        /// The row index.
        /// </param>
        /// <param name="columnName">
        /// The column name.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <typeparam name="T2">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T2"/>.
        /// </returns>
        public T2 GetCellChild<T2>(int rowIndex, string columnName, string searchProperties) where T2 : ICuitWinControl
        {
            int colIndex = this.GetColumnIndex(columnName);
            var control = this.GetCellChild<T2>(rowIndex, colIndex, searchProperties);
            return control;
        }

        /// <summary>
        /// The get cell child.
        /// </summary>
        /// <param name="rowIndex">
        /// The row index.
        /// </param>
        /// <param name="colIndex">
        /// The col index.
        /// </param>
        /// <typeparam name="T2">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T2"/>.
        /// </returns>
        public T2 GetCellChild<T2>(int rowIndex, int colIndex) where T2 : ICuitWinControl
        {
            return this.GetCellChild<T2>(rowIndex, colIndex, string.Empty);
        }

        /// <summary>
        /// The get cell child.
        /// </summary>
        /// <param name="rowIndex">
        /// The row index.
        /// </param>
        /// <param name="columnName">
        /// The column name.
        /// </param>
        /// <typeparam name="T2">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T2"/>.
        /// </returns>
        public T2 GetCellChild<T2>(int rowIndex, string columnName) where T2 : ICuitWinControl
        {
            return this.GetCellChild<T2>(rowIndex, columnName, string.Empty);
        }

        /// <summary>
        /// The get cell child.
        /// </summary>
        /// <param name="rowIndex">
        /// The row index.
        /// </param>
        /// <param name="colIndex">
        /// The col index.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <typeparam name="T2">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T2"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// </exception>
        public T2 GetCellChild<T2>(int rowIndex, int colIndex, string searchProperties) where T2 : ICuitWinControl
        {
            this._control.WaitForControlReady();

            WinCell winCell = new CuitWinCell(this._control, rowIndex, colIndex).UnWrap();

            object winControl;

            if (typeof(T2) == typeof(CuitWinButton))
            {
                winControl = new CuitWinButton(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinCalendar))
            {
                winControl = new CuitWinCalendar(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinCell))
            {
                winControl = new CuitWinCell(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinCheckBox))
            {
                winControl = new CuitWinCheckBox(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinCheckBoxTreeItem))
            {
                winControl = new CuitWinCheckBoxTreeItem(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinClient))
            {
                winControl = new CuitWinClient(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinColumnHeader))
            {
                winControl = new CuitWinColumnHeader(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinComboBox))
            {
                winControl = new CuitWinComboBox(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinCustom))
            {
                winControl = new CuitWinCustom(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinDateTimePicker))
            {
                winControl = new CuitWinDateTimePicker(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinEdit))
            {
                winControl = new CuitWinEdit(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinGroup))
            {
                winControl = new CuitWinGroup(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinHyperlink))
            {
                winControl = new CuitWinHyperlink(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinList))
            {
                winControl = new CuitWinList(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinListItem))
            {
                winControl = new CuitWinListItem(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinMenu))
            {
                winControl = new CuitWinMenu(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinMenuBar))
            {
                winControl = new CuitWinMenuBar(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinMenuItem))
            {
                winControl = new CuitWinMenuItem(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinPane))
            {
                winControl = new CuitWinPane(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinProgressBar))
            {
                winControl = new CuitWinProgressBar(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinRadioButton))
            {
                winControl = new CuitWinRadioButton(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinRow))
            {
                winControl = new CuitWinRow(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinRowHeader))
            {
                winControl = new CuitWinRowHeader(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinScrollBar))
            {
                winControl = new CuitWinScrollBar(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinSeparator))
            {
                winControl = new CuitWinSeparator(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinSlider))
            {
                winControl = new CuitWinSlider(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinSpinner))
            {
                winControl = new CuitWinSpinner(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinSplitButton))
            {
                winControl = new CuitWinSplitButton(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinStatusBar))
            {
                winControl = new CuitWinStatusBar(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinTable))
            {
                winControl = new CuitWinTable(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinTabList))
            {
                winControl = new CuitWinTabList(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinTabPage))
            {
                winControl = new CuitWinTabPage(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinText))
            {
                winControl = new CuitWinText(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinTitleBar))
            {
                winControl = new CuitWinTitleBar(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinToolBar))
            {
                winControl = new CuitWinToolBar(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinToolTip))
            {
                winControl = new CuitWinToolTip(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinTree))
            {
                winControl = new CuitWinTree(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinTreeItem))
            {
                winControl = new CuitWinTreeItem(winCell, searchProperties);
            }
            else if (typeof(T2) == typeof(CuitWinWindow))
            {
                winControl = new CuitWinWindow(winCell, searchProperties);
            }
            else
            {
                throw new Exception(string.Format("CreateControls: '{0}' not supported", typeof(T2).Name));
            }

            return (T2)winControl;
        }

        /// <summary>
        /// The get cell value.
        /// </summary>
        /// <param name="rowIndex">
        /// The row index.
        /// </param>
        /// <param name="colIndex">
        /// The col index.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetCellValue(int rowIndex, int colIndex)
        {
            return this._control.GetCell(rowIndex, colIndex).Value;
        }

        /// <summary>
        /// The get column index.
        /// </summary>
        /// <param name="columnName">
        /// The column name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int GetColumnIndex(string columnName)
        {
            this._control.WaitForControlReady();
            string[] columnuHeaders = this._control.GetColumnNames();
            int columnIndex = -1;
            for (int i = 0; i < columnuHeaders.Count(); i++)
            {
                if (columnuHeaders[i] == columnName)
                {
                    columnIndex = i;
                    break;
                }
            }

            return columnIndex;
        }

        /// <summary>
        ///     The get column names.
        /// </summary>
        /// <returns>
        ///     The <see cref="string[]" />.
        /// </returns>
        public string[] GetColumnNames()
        {
            this._control.WaitForControlReady();
            return this._control.GetColumnNames();
        }

        /// <summary>
        ///     The get row header names.
        /// </summary>
        /// <returns>
        ///     The <see cref="string[]" />.
        /// </returns>
        public string[] GetRowHeaderNames()
        {
            this._control.WaitForControlReady();
            return this._control.RowHeaders.Cast<string>().ToArray();
        }

        #endregion
    }
}